using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;

namespace BlazeApps.Library.Resources
{
    [Serializable()]
    public class ItemResource : Csla.BusinessBase<ItemResource>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Business Properties and Methods

        //declare members
        private Guid _id = Guid.NewGuid();
        private Guid _itemid = Guid.Empty;
        private Guid _resourceid = Guid.Empty;

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid id
        {
            get
            {
                CanReadProperty("id", true);
                return _id;
            }
        }

        public Guid ResourceID
        {
            get
            {
                CanReadProperty("resourceid", true);
                return _resourceid;
            }
            set
            {
                CanWriteProperty("resourceid", true);
                if (!_resourceid.Equals(value))
                {
                    _resourceid = value;
                    PropertyHasChanged("resourceid");
                }
            }
        }

        public Guid Itemid
        {
            get
            {
                CanReadProperty("Itemid", true);
                return _itemid;
            }
            set
            {
                CanWriteProperty("Itemid", true);
                if (!_itemid.Equals(value))
                {
                    _itemid = value;
                    PropertyHasChanged("Itemid");
                }
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {
        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
        }


        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in ItemResource
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("ItemResourceViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in ItemResource
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("ItemResourceAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in ItemResource
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("ItemResourceEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in ItemResource
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("ItemResourceDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private ItemResource()
        { /* require use of factory method */
        }

        public static ItemResource NewItemResource()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a ItemResource");
            return DataPortal.Create<ItemResource>();
        }

        public static ItemResource GetItemResource(Guid itemid, Guid resourceid)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a ItemResource");
            return DataPortal.Fetch<ItemResource>(new Criteria(itemid, resourceid));
        }

        public static void DeleteItemResource(Guid itemid, Guid resourceid)
        {
            if (!CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a ItemResource");
            DataPortal.Delete(new Criteria(itemid, resourceid));
        }

        public override ItemResource Save()
        {
            if (IsDeleted && !CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a ItemResource");
            else if (IsNew && !CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a ItemResource");
            else if (!CanEditObject())
                throw new System.Security.SecurityException("User not authorized to update a ItemResource");

            return base.Save();
        }

        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid Itemid;
            public Guid Resourceid;

            public Criteria(Guid itemid, Guid resourceid)
            {
                this.Itemid = itemid;
                this.Resourceid = resourceid;
            }
        }

        #endregion //Criteria

        #region Data Access - Create
        [RunLocal]
        protected override void DataPortal_Create()
        {
            ValidationRules.CheckRules();
        }

        #endregion //Data Access - Create

        #region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            DAL.AddParameter("@resourceid", criteria.Resourceid);

            using (SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetResourceByResourceID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                FetchObject(dr);
                ValidationRules.CheckRules();

                //load child object(s)
                FetchChildren(dr);
            }
        }

        private void FetchObject(SafeDataReader dr)
        {
            dr.Read();
            _itemid = dr.GetGuid("itemid");
            _resourceid = dr.GetGuid("resourceid");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #region Data Access - Insert
        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        private void ExecuteInsert()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.InsertItemResource.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@itemid", _itemid);
            DAL.AddParameter("@resourceid", _resourceid);
        }
        #endregion //Data Access - Insert

        #region Data Access - Update
        protected override void DataPortal_Update()
        {
            if (base.IsDirty)
            {
                ExecuteUpdate();
            }
        }

        private void ExecuteUpdate()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddUpdateParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.UpdateItemResource.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddUpdateParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@itemid", _itemid);
            DAL.AddParameter("@resourceid", _resourceid);
        }

        private void UpdateChildren(SqlConnection cn)
        {
        }
        #endregion //Data Access - Update

        #region Data Access - Delete
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(_itemid, _resourceid));
        }

        private void DataPortal_Delete(Criteria criteria)
        {
            ExecuteDelete(criteria);
        }

        private void ExecuteDelete(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddDeleteParameters(ref DAL, criteria);
            DAL.ExecuteNonQuery(Procs.DeleteItemResource.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddDeleteParameters(ref BA.DAL.DBUtility DAL, Criteria criteria)
        {
            DAL.AddParameter("@itemid", criteria.Itemid);
            DAL.AddParameter("@resourceid", criteria.Resourceid);
        }
        #endregion //Data Access - Delete
        #endregion //Data Access
    }
}
